from selenium import webdriver
import time
from bs4 import BeautifulSoup
import os
import urllib

url = 'https://mm.taobao.com/search_tstar_model.htm'

driver = webdriver.PhantomJS()
driver.set_window_size(1920, 2000)
driver.get(url)
time.sleep(1)

soup = BeautifulSoup(driver.page_source, 'lxml')
items = soup.find_all(class_='item')
for item in items:
    link = item.find('a').get('href')
    item_img = item.find('img')
    img = item_img.get('data-ks-lazyload') or item_img.get('src')
    name = item.find(class_='name').get_text()
    city = item.find(class_='city').get_text()
    print(link, img, name, city)
    dir_city = 'photos/' + city
    if not os.path.exists(dir_city):
        os.mkdir(dir_city)
    dir_name = dir_city + '/' + name
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)
    if img.startswith('//'):
        img = 'http:' + img
    filename = dir_name + '/' + img.split('/')[-1]
    if not os.path.exists(filename):
        urllib.request.urlretrieve(img, filename)

driver.close()